# -*- coding: utf-8 -*-
import os

from app import helper
from app.deal_data import DealData
from ui.main import *
from PyQt5.QtWidgets import QApplication, QMainWindow
import sys



class appWindow(QMainWindow):
    def __init__(self):
        super(appWindow, self).__init__()
        self.target_path = os.getcwd() + '/excel'
        # 初始化窗体，设置样式
        self.excel = None
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)
        self.setWindowTitle("小工具")

        # 设置按钮按钮点击事件
        self.ui.pushButton_open.clicked.connect(self.open)
        self.ui.pushButton_open_xls.clicked.connect(self.open_xls)
        self.ui.pushButton_save.clicked.connect(self.save)
        self.ui.frame_3.hide()
        self.ui.frame.hide()
        self.show()
        self.deal_data = DealData()
        self.deal_data.progress_signal.connect(self.progress_bar_update)
        self.deal_data.result_signal.connect(self.update_file)
        helper.clear_directory(self.target_path)

    def progress_bar_update(self,  progress):
        self.ui.progressBar.setValue(progress)

    def update_file(self, path):
        self.excel = path
        self.ui.frame.show()
    def open(self):
        self.deal_data.open_login()

    def open_xls(self):
        try:
            os.startfile(self.target_path)
        except Exception as e:
            print(f"错误: {str(e)}")



    def save(self):
        self.ui.frame.hide()
        self.ui.frame_3.show()
        self.progress_bar_update(0)
        self.deal_data.init()
        self.deal_data.start()




if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = appWindow()
    window.show()
    sys.exit(app.exec())
